package com.aiju.cash.core.api;

import com.aiju.utils.L;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.DialogHelper;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2017-03-16 16:06
 * @version: V1.0
 * @Description: 描述
 */
public abstract class SimpleCallback extends StringCallback {

    private DialogHelper mDialogHelper;

    public SimpleCallback(DialogHelper dialogHelper) {
        this.mDialogHelper = dialogHelper;
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        if (mDialogHelper != null) {
            mDialogHelper.dismissProgressDialog();
        }
        onDataFailure(404, "网络异常，请检查网络设置");
    }

    @Override
    public void onResponse(String response, int id) {
        if (mDialogHelper != null) {
            mDialogHelper.dismissProgressDialog();
        }
        DataJson dataJson = new DataJson(response);
        if (dataJson.isSuccess() || dataJson.isPaySuccess()) {
            onDataSuccess(dataJson.getData().toString());
        } else {
            onDataFailure(dataJson.getCode(), dataJson.getMsg());
        }
    }

    /**
     * 成功数据 json里的的key=data
     *
     * @param data
     */
    protected abstract void onDataSuccess(String data);

    /**
     * 数据错误
     */
    protected void onDataFailure(int code, String msg) {
        if (code == 404) {
            T.ToastNetError();
        }
        L.d("错误码：" + code + "\n错误信息：" + msg);
    }
}
